package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.system.Module;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.system.ModuleService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;
    @Autowired
    private ModuleService moduleService;

    //查询所有企业数据
    @RequestMapping(value="/list",name="查询所有企业数据")
    public String findAll(@RequestParam(name = "page",defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "2") int pageSize)  {
        //调用方法(使用分页插件)
        PageInfo<Company> pageInfo = companyService.findPage(pageNum, pageSize);
        request.setAttribute("page",pageInfo);
        return "company/company-list";
    }

    //进入企业新增页面
    @RequestMapping(value = "/toAdd",name = "进入企业新增页面")
    public String toAdd(){
        return "company/company-add";
    }

    //保存企业
    @RequestMapping(value = "/edit",name = "保存企业方法")
    public String edit(Company company){
        //company的id如果为空就是新增，反之就是修改
        if (StringUtils.isEmpty(company.getId())){
            //新增需要赋值id,使用UUID生成一个随机不重复id
            company.setId(UUID.randomUUID().toString());
            //调用方法,保存企业
            companyService.save(company);
        }else {
            companyService.update(company);
        }
        //重定向到列表界面
        return "redirect:/company/list.do";
    }

    //进入修改企业信息页面
    @RequestMapping(value = "/toUpdate",name = "进入修改企业信息页面")
    public String toUpdate(String id){
        //根据id查询企业
        Company company = companyService.findById(id);
        request.setAttribute("company",company);
        //修改页面和新增页面一样
        return "company/company-add";
    }

    //删除企业
    @RequestMapping(value ="/delete",name = "删除企业方法 ")
    public String delete(String id){
        companyService.deleteById(id);

        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/companyModule", name = "进入企业分配权限页面")
    public String companyModule(String companyId) {
        Company company = companyService.findById(companyId);
        request.setAttribute("company", company);
        return "company/company-module";
    }

    @RequestMapping(value = "/getZtreeNodes", name = "构造模块数据")
    @ResponseBody
    public List<Map> getZtreeNodes(String companyId) {
        //查询此 企业 拥有的菜单权限
        List<String> moduleIdList =  companyService.findModulesByCompanyId(companyId);

        //1.查询所有模块
        List<Module> moduleList = moduleService.findAll();

        //2.构造map集合
        List<Map> listMap = new ArrayList<>();
        Map map = null;

        //3.遍历所有模块
        for (Module module : moduleList) {
            //初始化map
            map = new HashMap();
            //添加数据
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

            //判断moduleIdList集合中是否包含module.getId()
            if (moduleIdList.contains(module.getId())){
                map.put("checked",true);
            }

            //存入list集合
            listMap.add(map);
        }
        //4.返回输入给ajax调用者
        return listMap;
    }

    @RequestMapping(value = "/updateCompanyModule",name = "实现分配权限")
    public String updateCompanyModule(String companyId,String moduleIds){
        //实现分配权限
        companyService.updateCompanyModule(companyId,moduleIds);

        return "redirect:/company/list.do";
    }

}
